﻿function CheckEmail(strMail) {
  if (strMail.length == 0) return false;
  var objReg = new RegExp("[A-Za-z0-9-_]+@[A-Za-z0-9-_]+[\.][A-Za-z0-9-_]");
  var IsRightFmt = objReg.test(strMail);
  var objRegErrChar = new RegExp("[^a-z0-9-._@]", "ig");
  var IsRightChar = (strMail.search(objRegErrChar) == -1);
  var IsRightLength = strMail.length <= 60;
  var IsRightPos = (strMail.indexOf("@", 0) != 0 && strMail.indexOf(".", 0) != 0 && strMail.lastIndexOf("@") + 1 != strMail.length && strMail.lastIndexOf(".") + 1 != strMail.length);
  var IsNoDupChar = (strMail.indexOf("@", 0) == strMail.lastIndexOf("@"));

  if (IsRightFmt && IsRightChar && IsRightLength && IsRightPos && IsNoDupChar) {
      return true;
  } else {
      return false;
    }
}

//获得Cookie的原始值
function GetCookie(name) {
  var cookies = document.cookie;
  var codeindex = cookies.indexOf(name);
  var startindex = codeindex + (name + "=").length;
  var vcode = cookies.substring(startindex, cookies.length);
  return vcode;
}

function refreshcode(target) {
  document.getElementById(target).src = "/tools/VerifyImagePage.aspx?time=" + Math.random();
}

function SetLang(langid) {
  ajaxRead("/tools/ajax.aspx?t=setLang&langid=" + langid, "GetSetLangRst(obj,0);");
}

function SetLangByFrame(langid) {
  ajaxRead("/tools/ajax.aspx?t=setLang&langid=" + langid, "GetSetLangRst(obj,1);");
}

function GetSetLangRst(obj,sid) {
  var res = obj.getElementsByTagName('result');
  var rst;
  if (res[0] != null && res[0] != undefined) {
    if (res[0].childNodes.length > 1) {
      rst = res[0].childNodes[1].nodeValue;
    } else {
    rst = res[0].firstChild.nodeValue;
    }
  }
  var tip = "";
  switch (rst) {
    case "0":
      tip = GetTipsByLanguage("error");
      break;
    case "1":
      tip = GetTipsByLanguage("set_success");
      break;
    default:
      tip = GetTipsByLanguage("error");
      break;
  }
  alert(tip);
  window.location.reload();
  if (sid == 1) {
    window.parent.frames.item('menuFrame').location.reload();
    window.parent.frames.item('managerFrame').location.reload();
  }
}

function GetTipsByLanguage(action) {
  var langid = parseInt(GetCookie("lan"));  
  var tips = "";
  
  switch (langid) {
    case 0:
      switch (action) {
        case "error":
          tips = "对不起，服务器忙，请稍后再试。";
          break;
        case "set_success":
          tips = "设置成功";
          break;
        default:
          break;
      }
      break;
    case 1:
      switch (action) {
        case "error":
          tips = "Server Error";
          break;
        case "set_success":
          tips = "Operation Success";
          break;
        default:
          break;
      }
      break;
    case 2:
      break;
    case 3:
      break;
    default:
      switch (action) {
        case "error":
          tips = "对不起，服务器忙，请稍后再试。";
          break;
        case "set_success":
          tips = "设置成功";
          break;
        default:
          break;
      }
      break;
  }
  return tips;
}

//设置复选框
//具体操作过程与选项类似
//在新的DIV中 输出数据
//再根据下拉列表与原有数据量 设置复选框数据
function InitCheckBoxData(inputID, chbName, chbIDPrefix) {
  var v = document.getElementById(inputID).value;
  var values = [];
  if (v.indexOf(',') != -1) {
    values = v.split(',');
  } else {
    values[0] = v;
  }
  var cbList = document.getElementsByName(chbName);
  for (var i = 0; i < cbList.length; i++) {
    var v = document.getElementById(chbIDPrefix + i).value;
    for (var j = 0; j < values.length; j++) {
      if (v == values[j]) {
        document.getElementById(chbIDPrefix + i).checked = true;
      }
    }
  }
}

function InitItemCountSlt(target, defaultvalue, startno, endno) {
  var resContainer = document.getElementById(target);
  resContainer.length = 0;
  for (var i = startno; i <= endno; i++) {
    resContainer.options.add(new Option(i, i));
  }
  resContainer.value = defaultvalue;
}

function selectAll(objId) {
    var allSelectControl = $("#" + objId + " input[type='checkbox']");
    $.each(allSelectControl, function (n, value) {
        if (value.checked == false) {
            value.checked = true;
        }
    });
}
function inverseAll(objId) {
    var allSelectControl = $("#" + objId + " input[type='checkbox']");

    $.each(allSelectControl, function (n, value) {
        if (value.checked == false) {
            value.checked = true;
        } else {
            value.checked = false;
        }
    });
}

function confirmArrayId(objId, hideObjId,seperatorChar) {
    var idArray = '';
    var allSelectControl = $("#" + objId + " input[type='checkbox']");
    $.each(allSelectControl, function (n, value) {
        if (value.checked == true) {
            idArray = idArray + value.value + seperatorChar;
        }
    });

    $("#" + hideObjId).attr("value", idArray.substring(0, idArray.length - 1));
}